package org.scboy.dbms.service.imp;

import java.util.List;

import org.scboy.coobi.sql.factory.EntityFactory;
import org.scboy.dbms.dao.BaseDictionaryDao;
import org.scboy.dbms.dao.BaseDictionaryInfoDao;
import org.scboy.dbms.dao.BaseDictionaryTypeJoinDao;
import org.scboy.dbms.dao.BaseDictionaryTypeValueDao;
import org.scboy.dbms.loads.LoadDictionary;
import org.scboy.dbms.service.DictionaryService;
import org.scboy.utils.entitys.coobi.Model;
import org.scboy.utils.entitys.coobi.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.hutool.core.util.StrUtil;

/**
 * 字典业务类
 * @author yanghao
 *
 */
@Service
public class DictionaryServiceImpl implements DictionaryService {

	@Autowired
	EntityFactory factory;

	@Autowired
	LoadDictionary dicCache;
	
	/**
	 * 分页查询字典
	 * @param model 查询参数
	 * @return 分页结果
	 */
	public Page page(Model model){
		
		BaseDictionaryDao dao=factory.getDao(BaseDictionaryDao.class);
		
		return dao.page(model);
	}
	
	public List<Model> list(){
		BaseDictionaryDao dao=factory.getDao(BaseDictionaryDao.class);
		return dao.select(Model.create().add("activity", true));
	}
	
	/**
	 * 新增字典
	 * @param model
	 * @return
	 */
	public boolean add(Model model){
		BaseDictionaryDao dao=factory.getDao(BaseDictionaryDao.class);
		if(dao.count(Model.create().add("code", model.getString("code")))>0){
			return false;
		}
		model.add("code", model.getString("code").trim());
		if(dao.insert(model)>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 更新字典
	 * @param model
	 * @return
	 */
	public boolean update(Model model){
		BaseDictionaryDao dao=factory.getDao(BaseDictionaryDao.class);
		//判断编码是否已经存在，不包含自己的编码。
		if(StrUtil.isEmpty(model.getString("code"))){
			return false;
		}
		if(dao.update(model)>0){
			return true;
		}else{
			return false;
		}
	}

	/**
	 * 删除字典
	 * @param id
	 * @return
	 */
	@Transactional
	public boolean delete(String code){
		BaseDictionaryDao dao=factory.getDao(BaseDictionaryDao.class);

		BaseDictionaryInfoDao infodao=factory.getDao(BaseDictionaryInfoDao.class);
		BaseDictionaryTypeJoinDao joinDao=factory.getDao(BaseDictionaryTypeJoinDao.class);
		BaseDictionaryTypeValueDao tvDao=factory.getDao(BaseDictionaryTypeValueDao.class);

		Model params=Model.create().add("code", code);
		
		//获取所有的字典配置
		List<Model> infos=infodao.select(Model.create().add("code", code).addRc("id"));
		
		//删除配置的字典值
		infodao.delete(params);
		//删除关联的转换类型
		joinDao.delete(params);
		//删除管理的转换值
		tvDao.delete(params);
		
		int count =dao.delete(params);
		if(count>0){
			//清除字典的缓存值数据
			for (Model model : infos) {
				dicCache.removeCache(model.getString("id"));
			}
			return true;
		}else{
			return false;
		}
	}
	/**
	 * 获取字典
	 * @param code
	 * @return
	 */
	public Model get(String code){
		BaseDictionaryDao dao=factory.getDao(BaseDictionaryDao.class);
		return dao.get(Model.create().add("code", code));
	}
	
	/**
	 * 启用禁用字典
	 * @param id ID
	 * @return
	 */
	public boolean checkState(String code) {
		BaseDictionaryDao dao=factory.getDao(BaseDictionaryDao.class);
		Model model = get(code);
		if (model == null) {
			return false;
		}
		model.add("activity", !model.getBoolean("activity"));
		int resultCount = dao.update(model);
		if (resultCount > 0) {
			return true;
		} else {
			return false;
		}
	}
}


